package com.magnificent.atlantis.client.ui.planner.panels.alert;

import com.magnificent.atlantis.client.AtlantisConstants;
import com.magnificent.atlantis.client.data.alert.AbstractAlert;
import com.magnificent.commons.utils.DateUtils;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

/**
 * User: Babanin
 * Date: 20.02.2009
 * Time: 15:25:41
 */
public class AlertsTableRenderer extends DefaultTableRenderer {

    private static Date CURRENT_DAY = new Date();

    // -------------- implements javax.swing.table.CreatorTableCellRenderer
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        AbstractAlert abstractAlert = (AbstractAlert) value;

        String strValue = "";
        switch (column) {
            case 0:
                strValue = abstractAlert.getTitle();
                break;
            case 1:
                strValue = AtlantisConstants.DATE_TIME_FORMAT.format(abstractAlert.getDate());
                break;
            case 2:
                strValue = AtlantisConstants.DATE_TIME_FORMAT.format(abstractAlert.getGenerationDate());
                break;
            case 3:
                strValue = abstractAlert.isAutomatic() ? AtlantisConstants.EVENT_AUTOMATIC : AtlantisConstants.EVENT_MANUAL;
                break;
        }

        label.setText(" " + strValue);
        if (DateUtils.isSameDay(CURRENT_DAY, abstractAlert.getDate())) {
            // now

            if (isSelected) {
                label.setBackground(Color.RED);
                label.setForeground(Color.WHITE);
            } else {
                label.setBackground(Color.WHITE);
                label.setForeground(Color.RED);
            }
        } else if (CURRENT_DAY.getTime() - abstractAlert.getDate().getTime() < 0) {
            // future

            if (isSelected) {
                label.setBackground(Color.GREEN.darker().darker());
                label.setForeground(Color.WHITE);
            } else {
                label.setBackground(Color.WHITE);
                label.setForeground(Color.GREEN.darker().darker());
            }
        } else {
            // past

            if (isSelected) {
                label.setBackground(Color.GRAY);
                label.setForeground(Color.WHITE);
            } else {
                label.setBackground(Color.WHITE);
                label.setForeground(Color.GRAY);
            }
        }

        return label;
    }
}
